// This is the main DLL file.

#include "stdafx.h"

#include "OpenGLDiagLibrary.h"

#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "User32.lib")
#pragma comment (lib, "Gdi32.lib")

namespace OpenGLDiagLibrary {

		// Windows Proc Function
		LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
		{
			switch (message)
			{
			default:
				return DefWindowProc(hWnd, message, wParam, lParam);
				break;
			}
		}


		// returns the currently installed OS - returns nullptr when OS lower than WindowsXP
		String^ Probing::GetInstalledOS()
		{
			OSVERSIONINFO osvi;
			
			ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
			osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

			// get OS version
			if(GetVersionEx(&osvi) == FALSE)
				return nullptr;

			// transform version to string
			if(osvi.dwMajorVersion == 5)
			{
				return gcnew String("Windows XP");
			}
			else if(osvi.dwMajorVersion == 6)
			{
				switch(osvi.dwMinorVersion)
				{
				case 0:
					return gcnew String("Windows Vista");
				case 1:
					return gcnew String("Windows 7");
				case 2:
					return gcnew String("Windows 8");
				case 3:
					// to detect this the application needs manifesting for Windows 8.1
					// URL Source: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724834(v=vs.85).aspx
					return gcnew String("Windows 8.1");
				}
			}

			return nullptr;
		}

		String^ Probing::GetOpenGLVersion()
		{
			// create empty window
			WNDCLASSEX wcex;
			TCHAR szWindowClass[] = L"win32app";


			wcex.cbSize = sizeof(WNDCLASSEX);
			wcex.style          = CS_HREDRAW | CS_VREDRAW;
			wcex.lpfnWndProc    = WndProc;
			wcex.cbClsExtra     = 0;
			wcex.cbWndExtra     = 0;
			wcex.hInstance      = ::GetModuleHandle(NULL);
			wcex.hIcon          = LoadIcon(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPLICATION));
			wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
			wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
			wcex.lpszMenuName   = NULL;
			wcex.lpszClassName  = szWindowClass;
			wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

			 if (!RegisterClassEx(&wcex))
			 {
				 return nullptr;
			 }


			HWND hWnd = ::CreateWindowW(szWindowClass, L"OpenGL", SW_HIDE, 0, 0, 640, 480, NULL, NULL, ::GetModuleHandle(NULL), NULL);
			if(!hWnd)
				return nullptr;

			PIXELFORMATDESCRIPTOR pfd =
                {
                        sizeof(PIXELFORMATDESCRIPTOR),
                        1,
                        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
                        PFD_TYPE_RGBA,            //The kind of framebuffer. RGBA or palette.
                        32,                        //Colordepth of the framebuffer.
                        0, 0, 0, 0, 0, 0,
                        0,
                        0,
                        0,
                        0, 0, 0, 0,
                        24,                        //Number of bits for the depthbuffer
                        8,                        //Number of bits for the stencilbuffer
                        0,                        //Number of Aux buffers in the framebuffer.
                        PFD_MAIN_PLANE,
                        0,
                        0, 0, 0
                };

				HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

                int  letWindowsChooseThisPixelFormat;
                letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd); 
                SetPixelFormat(ourWindowHandleToDeviceContext,letWindowsChooseThisPixelFormat, &pfd);

                HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
                wglMakeCurrent (ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

				String^ openGLVersion = gcnew String((char*)glGetString(GL_VERSION));
                wglDeleteContext(ourOpenGLRenderingContext);

				::DestroyWindow(hWnd);
				return openGLVersion;
		}

}
